package com.apitest;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.apitest.config.Parameters;
import com.apitest.moudle.Attributes;
import com.apitest.moudle.Base;
import com.apitest.moudle.CaseDesign;
import com.apitest.moudle.ContentType;
import com.apitest.utils.ApiKit;
import com.apitest.utils.Descartes;
import com.apitest.utils.FsKit;
import com.apitest.utils.TemplateUtils;
import com.fasterxml.jackson.databind.ObjectMapper;


import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.restassured.response.Response;
import org.yaml.snakeyaml.Yaml;


import java.io.File;
import java.io.IOException;
import java.util.*;


public class StepDefinitions {
    public static String api = "";
    public static Map<String, Object> params = new HashMap<>();
    public static Map<String, Object> cookies = new HashMap<>();
    public static Map<String, Object> headers = new HashMap<>();
    public static Map<String, Object> asserts = new HashMap<>();
    ArrayList<ArrayList<Attributes>> descartesParams = new ArrayList<>();

    /**
     *
     * @param
     */
    @After
    public void setGenerator(Scenario scenario) {
        descartesParams.forEach(cases->{
            Map<String,Object> param =  ApiKit.parseAttr(cases);
            String json =  JSONObject.toJSONString(cases);
            String list  =  JSONPath.read(json,"$..valid").toString();
            boolean isTrue = !list.contains("false");
            String  TCName = String.format("TC-%s",descartesParams.indexOf(cases)+1);
            Base  base =  new Base(scenario.getName(),
                    TCName,
                    api,
                    "GET",
                    headers,
                    cookies,
                    param
            );
            base.setIsTrue(""+isTrue);
            base.setAsserts(asserts);
            try {
                String code  = TemplateUtils.parseTestCodeTemplate(base);
                FsKit.writeCode(FsKit.projectPath()+ File.separatorChar+"testFile"+File.separatorChar,
                        TCName+".feature",
                        code);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }


    @Given("Headers")
    public void setHeaders(Map<String, Object> paramsTmp) {
        headers = paramsTmp;
    }
    @Given("Cookies")
    public void setCookies(Map<String, Object> paramsTmp) {
        cookies = paramsTmp;
    }
    @Given("^API \"(.*?)\"$")
    public void setHeaders(String apiPath) {
        api = apiPath;
    }
    @Given("Assert")
    public void setBody(Map<String,String> map) {
        Parameters.assertMap = map;
        asserts.putAll(map);
    }
    @Given("CONTENT_TYPE_IN_MULTIPART")
    public void setContentType() {
        Parameters.setContentType(ContentType.FILE);
    }
    @Given("ParamCase")
    public void setParamsCaseYaml(String yamlContent) {
        // 替换变量
        Yaml yaml = new Yaml();
        params= (Map) yaml.load(yamlContent);
        ArrayList<CaseDesign> arr = new ArrayList();
        params.forEach((k,v)->{
           CaseDesign cd =  new CaseDesign();
           if(v!=null){
               cd.setName(k);
               Map<Object,String> map = (Map) v;
               map.forEach((key,value)->{
                   System.out.println(value);
                   if((Boolean) key){
                       cd.setValid(Arrays.asList(value.split(",")));
                   }else {
                       cd.setInValid(Arrays.asList(value.split(",")));
                   }
               });
           }
            arr.add(cd);
        });
        descartesParams =  Descartes.parse(arr);
    }



}
